package com.liujianlei.forum_system.interceptor;

import com.liujianlei.forum_system.config.AppConfig;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.servlet.HandlerInterceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

/**
 * @author tongchen
 * @create 2023-06-18 10:46
 */
@Slf4j
public class LoginInterceptor implements HandlerInterceptor {
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        log.info("拦截器拦截到请求...");
        //获取session
        HttpSession session = request.getSession(false);
        if(session!=null&&session.getAttribute(AppConfig.USER_SESSION)!=null){
            //用户已登录
            return true;
        }
        //设置强制跳转
        response.sendRedirect("/sign-in.html");
        return false;
    }
}
